package cn.tedu.peipei.user.service.impl;

import cn.tedu.peipei.base.enums.StatusCode;
import cn.tedu.peipei.base.exception.ServiceException;
import cn.tedu.peipei.user.mapper.UserMapper;
import cn.tedu.peipei.user.pojo.dto.UserLoginParam;
import cn.tedu.peipei.user.pojo.dto.UserQueryParam;
import cn.tedu.peipei.user.pojo.dto.UserSaveParam;
import cn.tedu.peipei.user.pojo.entity.User;
import cn.tedu.peipei.user.pojo.vo.UserVO;
import cn.tedu.peipei.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public UserVO login(UserLoginParam userLoginParam) {
        log.debug("用户登录，参数：{}",userLoginParam);
        UserVO userVO = userMapper.selectByUsername(userLoginParam.getUsername());
        if(userVO==null){
            //主业务如果遇到了问题,需要抛出业务层异常,而不是直接给前端返回结果
            //Controller层才是给前端返回结果的那一层
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if(!userVO.getPassword().equals(userLoginParam.getPassword())){
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        return userVO;
    }
    @Override
    public void saveUser(UserSaveParam userSaveParam) {
        log.debug("保存用户业务:userSaveParam={}",userSaveParam);
        User user = new User();
        BeanUtils.copyProperties(userSaveParam,user);
        //如果没有用户id,说明是新增操作
        if(user.getId()==null){
            user.setPassword("123456");
            user.setCreateTime(new Date());
            userMapper.insert(user);
        }else{//如果有用户id,说明是编辑更新操作
            //TODO:修改操作
            user.setUpdateTime(new Date());
            userMapper.update(user);
        }
    }

    @Override
    public List<UserVO> selectUserList(UserQueryParam userQuery) {
        log.debug("查询用户业务:userQuery={}",userQuery);
        List<UserVO> list = userMapper.selectUserList(userQuery);
        log.debug("查询结果:list={}",list);
        return list;
    }

    @Override
    public void resetPassword(Long userId) {
        log.debug("重置密码业务:userId={}",userId);
        User user = new User();
        user.setId(userId);
        user.setPassword("root");
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void updateStatus(Long userId, String status) {
        log.debug("更新用户状态业务:userId={},status={}",userId,status);
        User user = new User();
        user.setId(userId);
        user.setStatus(status);
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void deleteUser(Long userId) {
        log.debug("删除用户业务:userId={}",userId);
        userMapper.deleteById(userId);
    }

    @Override
    public List<UserVO> selectAuditList(Long parentId) {
        log.debug("查询审批人列表业务:parentId={}",parentId);
        //1.准备一个空集合,用来保存查出来的多个审批人
        ArrayList<UserVO> userVOList = new ArrayList<>();
        //2.根据传过来的上级id,先把直属领导查出来
        UserVO auditUser1 = userMapper.selectById(parentId);
        userVOList.add(auditUser1);
        //3.如果有直属领导,且直属领导还有领导,继续查询
        if(auditUser1 != null && auditUser1.getParentId() != null){
            UserVO auditUser2 = userMapper.selectById(auditUser1.getParentId());
            userVOList.add(auditUser2);
        }
        return userVOList;
    }
}
